#include "mysocket.h"
#include<QFile>
#include<QMessageBox>
#include<QFileInfo>
#include<QHostAddress>
MySocket::MySocket(QObject *parent) : QObject(parent)
{
    socket=new QTcpSocket(this);

}

MySocket::~MySocket()
{

}


void MySocket::connectSever(const QString &hostName, quint16 port)
{
   socket->connectToHost(QHostAddress(hostName),port);
   connect(socket,&QTcpSocket::connected,this,&MySocket::sendConnect);
   connect(socket,&QTcpSocket::disconnected,this,[=](){
       socket->close();
       socket->deleteLater();
       emit gameover();
   });

}


void MySocket::sendFile(QString filepath11)
{
   QFile file(filepath11);
   bool isok=file.open(QIODevice::ReadOnly);
   QFileInfo info(filepath11);
   int fileSize=info.size();


   while(!file.atEnd())
   {
       static int num=0;
       if(num==0)
       {
           socket->write((char *)&fileSize,4);
       }
       QByteArray array=file.readLine();
       num+=array.size();

       int percent=(num*100/fileSize);
       emit sendFileProcess(percent);
       socket->write(array);
   }
//    QFile file(path);

//    QFileInfo info(path);
//    int fileSize =info.size();
//    file.open(QIODevice::ReadOnly);
//    while(!file.atEnd())
//    {

//        static int num=0;
//        if(num==0)
//        {
//            socket->write((char*)&fileSize,4);
//        }
//        QByteArray line=file.readLine();
//        num+=line.size();
//        int percent=(num*100/fileSize);
//        emit curPercent(percent);

//        socket->write(line);


//    }

}
